import os
from xlwt import Workbook
import re

path = os.getcwd()


def main():
    print("start")


def read1(fp):
    result = []
    s = ''
    with open(fp) as f:
        s = f.read()
        s = s.replace(' ', '')
        s = s.replace('\t', '')
    last = 0
    i = 0
    while i != -1:
        last = i + 1
        i = s.find(';', last)
        result.append(s[last:i])
    return result


def read2(fp):
    result = []
    with open(fp) as f:
        s = '#'
        while s:
            s = f.readline()
            result.append(s)
    return result


def save_file1(l):
    ws = Workbook(encoding='utf-8')
    w = ws.add_sheet('data')


def save_file2(l):
    print(l)
    ws = Workbook()
    w = ws.add_sheet('data')
    row = 0
    for s in l:
        if s.find(';') != -1:
            i = s.find(';')
            s1 = clear_2(s[:i])
            s2 = s[i + 1:]
            w.write(row, 0, s1)
            w.write(row, 1, s2)
            row = row + 1
    ws.save('err2.xls')


def save_file1(l):
    ws = Workbook()
    w = ws.add_sheet('data')
    row = 0
    for s in l:
        print("row:"+s)
        if s.find('\n') != -1:
            s = s.strip('\n').strip('\t')
            i = s.find('\n')
            s1 = s[:i]
            s2 = clear_2(s[i + 1:])
            print("s1"+s1)
            print("s2" + s2)
            w.write(row, 0, s2)
            w.write(row, 1, s1)
            row = row + 1
    ws.save('err1.xls')


def clear_2(s):
    s = s.replace('=', '')
    s = s.replace('-', '')
    s = s.replace(' ', '')
    s = s.replace(';', '')
    pattern = re.compile(r'\d+')
    for ss in pattern.findall(s):
        s = s.replace(ss, '')
    return s


if __name__ == '__main__':
    main()
    err1 = path + '/err1.txt'
    err2 = path + '/err2.txt'

    # e1 = read1(err1)
    # print(e1)
    # save_file1(e1)
    e2 = read2(err2)
    print(e2)
    save_file2(e2)
